package com.apes.scm.account.ledger.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttribute;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.LogicalUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：往来总账帐目类型
 *
 * @author xul
 * @create 2018-11-03 14:13
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LedgerAccount.CONST_TABLE_NAME, indexes = {@Index(name = "u_acc_ledger_account", columnList = "ledger_group_id, usage", unique = true)})
public class LedgerAccount extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_ledger_account";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "往来总账类型")
    @Comment("帐目类型编号")
    private String id;

    @Column(name = "name", nullable = false, length = 60)
    @Comment("名称")
    private String name;

    @Column(name = "show", columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    @Comment("是否显示")
    private boolean show = true;

    @Column(name = "sequence")
    @Comment("显示顺序")
    private int sequence = 0;

    @Column(name = "minus_", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("是否允许负数")
    private boolean minus = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ledger_group_id", referencedColumnName = "id", nullable = false)
    @Comment("帐目类型分组")
    private LedgerGroup ledgerGroup;

    @States(name = "帐目用途", values = {
            @State(id = "unsettled", name = "未清项"),
            @State(id = "freeze", name = "冻结项"),
            @State(id = "invoiced", name = "已到票"),
            @State(id = "settled", name = "已清项"),
            @State(id = "provisional", name = "暂估项")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "usage", nullable = false, referencedColumnName = "id")
    private BusinessAttributeValue usage;

}
